package TUILayer;//makes this class a part of the TUILayer package
import java.util.ArrayList;
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.Person;

public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    
    //constructor which does nothing
    public AddressBookUI()
    {
       addrCtr = new AddressCtr();
    }
    
    public void start()
    {
       
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       ControlLayer.AddressCtr actr =new AddressCtr();
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu();
           int i_id;

           switch (choice) {
               case 1:
                   getPerson();

                   break;
               case 2:
                   createPerson();

                   break;
               case 3:
                   deletePerson();

                   break;
               case 4:
                   updatePerson();

                   break;
               case 5:
                   listAllPersons();
               case 6:
                   exit = true;
                   break;
               default:

                   System.out.println("Not valid choice.");
                   exit = true;

                   break;
           }
       }//end while
    }
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
        }
        
    //the method reads id for a person and return
    // 
    public int inputPersonsID()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try {
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }

    /* Custom creation of input instead of redundant inputThis inputThat functions */
    public String keyboardInput(String description) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(description);
        String keyboard_input = keyboard.nextLine();
        return keyboard_input;
    }

    private void getPerson()
    {//is to be changed when the address book controller is completed
        int id = inputPersonsID();
        Person p = addrCtr.getPerson(id);
        if(p != null) {
            System.out.println(p.getName());
            System.out.println(p.getAddress());
            System.out.println(p.getCity());
            System.out.println(p.getEmail());
            System.out.println(p.getPhone());
            System.out.println(p.getPostalCode());
        }
    }

    private void listAllPersons()
    {
        ArrayList<Person> persons = addrCtr.listAllPersons();

        for (Person pp : persons) {
            if(pp != null) {
                System.out.println(pp.getName());
                System.out.println(pp.getAddress());
                System.out.println(pp.getCity());
                System.out.println(pp.getEmail());
                System.out.println(pp.getPhone());
                System.out.println(pp.getPostalCode());
                System.out.println("------------");
            }
        }
    }

    private void createPerson()
    {
        String i_name = keyboardInput("New name:");
        String i_addr = keyboardInput("New address:");
        String i_postal = keyboardInput("New postal code:");
        String i_city = keyboardInput("New city:");
        String i_phone = keyboardInput("New phone:");
        String i_email = keyboardInput("New email:");

        addrCtr.createPerson(i_name, i_addr, i_postal, i_city, i_phone, i_email);
    }

    private void updatePerson()
    {
        String i_id_pre = keyboardInput("ID to Update:");

        int i_id = Integer.parseInt(i_id_pre);

        String i_name = keyboardInput("New name:");
        String i_addr = keyboardInput("New address:");
        String i_postal = keyboardInput("New postal code:");
        String i_city = keyboardInput("New city:");
        String i_phone = keyboardInput("New phone:");
        String i_email = keyboardInput("New email:");

        addrCtr.updatePersonById(i_id, i_name, i_addr, i_postal, i_city, i_phone, i_email);
    }

    private void deletePerson() {
        int id = Integer.parseInt(keyboardInput("ID to Delete:"));
        addrCtr.deletePersonById(id);
    }
}
            
        
